#include <iostream>
#include <vector>
#include <utility>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "randomgraph.h"
#include "randomtree.h"
#include "randombiparti.h"
#include "RandomSmallCover.h"
#include "FluxGraph.h"
#define NB_MEASURES 10

using namespace std;


vector<vector<int> >
compute_measures(vector<pair<int,struct timeval*> > measures) {
	int sec, ret, ret2, usec;
	int val = -1;
	int min_sec = 0, average_sec = 0, max_sec = 0,
		min_usec = 0, average_usec = 0, max_usec = 0;
	struct timeval tmp;

	vector<vector<int> > m = vector<vector<int> >();
	for(int i=0; i<measures.size();i+=NB_MEASURES) {
		val 	= measures[i].first;
		min_sec = 0;
		min_usec = 0;
		average_sec = 0;
		average_usec = 0;
		max_sec = 0;
		max_usec = 0;

		for(int j=0; j<NB_MEASURES; j++) {
			tmp.tv_sec = measures[i+j].second->tv_sec;
			tmp.tv_usec = measures[i+j].second->tv_usec;

			usec = (tmp.tv_usec<0)?1-tmp.tv_usec:tmp.tv_usec;
			ret  = (tmp.tv_usec<0)?-1:0;
			ret2 = ((usec+average_usec)<1000000)?0:1;
			sec  = tmp.tv_sec + ret;

			if ((min_sec == 0 && min_usec == 0) || ((sec < min_sec) || (sec == min_sec && usec < min_usec))) {
				min_sec = sec;
				min_usec = usec;
			}
			if ((max_sec == 0 && max_usec == 0) || ((sec > max_sec) || (sec == max_sec && usec > max_usec))) {
				max_sec = sec;
				max_usec = usec;
			}
			average_sec  += sec + ret2;
			average_usec += usec;
		}

		int tab[] = {val,
					min_sec*1000000+min_usec,
					(average_sec*1000000 + average_usec)/NB_MEASURES,
					max_sec*1000000+max_usec};
		m.push_back(vector<int>(tab,tab+sizeof(tab)/sizeof(int)));
	}
	return m;
}

struct timeval* createRandomGraph(int nb) {

	struct timeval start,end;
	struct timeval* result = (struct timeval*)malloc(sizeof(struct timeval));

	gettimeofday(&start,NULL);
	Graph * rg = new RandomSmallCover(nb,nb/10,0.5);
	gettimeofday(&end,NULL);
	delete(rg);

	result->tv_sec = end.tv_sec - start.tv_sec;
	result->tv_usec = end.tv_usec - start.tv_usec;

	return result;
}

vector<pair<int,struct timeval*> > testRandomGraph() {

	vector<pair<int,struct timeval*> > m = vector<pair<int,struct timeval*> >();
	int myints[] = {10,20,50,100,250,500,1000,2500};
	vector<int> times = vector<int>(myints, myints + sizeof(myints) / sizeof(int));

	for (int i=0; i<times.size(); i++) {
		for (int j=0; j<NB_MEASURES; j++) {
			m.push_back(pair<int,struct timeval*>(times[i],NULL));
		}
	}

	for(int i=0; i < m.size(); i++) {
		m[i].second = createRandomGraph(m[i].first);
	}

	return m;
}



int main(int argc, char** argv)
{
  /*	cout << "Programme de test de performances des algo " <<
			"de génération de graphe et des algo de détection " <<
			"de couverture" << endl << "la probabilitée est " <<
			"fixée par defaut a 0.5, chaque mesure est effectuée " <<
			"10 fois"<< endl;*/

	vector<pair<int,struct timeval*> > measures = testRandomGraph();

	vector<vector<int> > m = compute_measures(measures);

	for (int i=0; i<m.size(); i++) {
		int min =  m[i][1];
		int average = m[i][2];
		int max = m[i][3];

		/*	cout << "Size : " << m[i][0] << " ";
			cout << "Time min : " << min << " average : " << average << " max : " << max << endl;*/
		cout << "Size : " << m[i][0] << " ";
		cout << "average : " << average << endl;
	}

	//	cout << "Test de génération de graphe avec RandomGraph" << endl;

}
